package com.fr.schedule.output;

import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.XMLable;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.file.ConfigManager;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.report.ReportWebAttr;
import com.fr.report.web.EmailManager;
import com.fr.schedule.ScheduleContext;
import com.fr.schedule.dao.FieldMapper;
import com.fr.schedule.dao.JdbcDaoManager;
import com.fr.schedule.dao.ObjectMapper;
import java.io.File;
import javax.mail.MessagingException;

public class EmailNotification
  implements XMLable
{
  public static final String XML_TAG = "EmailNotification";
  public static final String TABLE_NAME = "fr_schedule_email";
  private long id = -1L;
  private String toAddress = null;
  private String ccAddress = null;
  private String bccAddress = null;
  private String subject;
  private String bodyContent;
  public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper("fr_schedule_email", new FieldMapper[] { new FieldMapper("id", 4, "id"), new FieldMapper("toAddress", 12, "toAddress"), new FieldMapper("ccAddress", 12, "ccAddress"), new FieldMapper("bccAddress", 12, "bccAddress"), new FieldMapper("subject", 12, "subject"), new FieldMapper("bodyContent", 12, "bodyContent") });

  public EmailNotification()
  {
  }

  public EmailNotification(String paramString1, String paramString2, String paramString3, String paramString4, String paramString5)
  {
    this.toAddress = paramString1;
    this.ccAddress = paramString2;
    this.bccAddress = paramString3;
    this.subject = paramString4;
    this.bodyContent = paramString5;
  }

  public JSONObject createJSONConfig()
    throws JSONException
  {
    JSONObject localJSONObject = new JSONObject();
    localJSONObject.put("id", getId());
    localJSONObject.put("toAddress", getToAddress());
    localJSONObject.put("ccAddress", getCcAddress());
    localJSONObject.put("bccAddress", getBccAddress());
    localJSONObject.put("subject", getSubject());
    localJSONObject.put("bodyContent", getBodyContent());
    return localJSONObject;
  }

  public static EmailNotification analyzeJSON(JSONObject paramJSONObject)
  {
    EmailNotification localEmailNotification;
    try
    {
      localEmailNotification = null;
      if (paramJSONObject.has("id"))
        localEmailNotification = (EmailNotification)ScheduleContext.getJdbcDaoManager().load(EmailNotification.class, paramJSONObject.getLong("id"));
      else
        localEmailNotification = new EmailNotification();
      String str1 = paramJSONObject.getString("toAddress");
      String str2 = paramJSONObject.getString("ccAddress");
      String str3 = paramJSONObject.getString("bccAddress");
      String str4 = paramJSONObject.getString("subject");
      String str5 = paramJSONObject.getString("bodyContent");
      localEmailNotification.setToAddress(str1);
      localEmailNotification.setCcAddress(str2);
      localEmailNotification.setBccAddress(str3);
      localEmailNotification.setSubject(str4);
      localEmailNotification.setBodyContent(str5);
      return localEmailNotification;
    }
    catch (JSONException localJSONException)
    {
      localJSONException.printStackTrace();
    }
    return null;
  }

  public void sendEmail(File[] paramArrayOfFile)
    throws EmailException
  {
    if (StringUtils.isNotBlank(getToAddress()))
      try
      {
        EmailManager localEmailManager = getEmailManager();
        if (localEmailManager == null)
          throw new EmailException("Email Manager is not Configed!");
        localEmailManager.send(getToAddress(), getCcAddress(), getBccAddress(), getSubject(), getBodyContent(), paramArrayOfFile);
      }
      catch (MessagingException localMessagingException)
      {
        throw new EmailException(localMessagingException);
      }
  }

  public static EmailManager getEmailManager()
  {
    EmailManager localEmailManager = null;
    ReportWebAttr localReportWebAttr = FRContext.getConfigManager().getReportWebAttr();
    if (localReportWebAttr == null)
    {
      localReportWebAttr = new ReportWebAttr();
      FRContext.getConfigManager().setReportWebAttr(localReportWebAttr);
    }
    localEmailManager = localReportWebAttr.getEmailManager();
    if (localEmailManager == null)
    {
      localEmailManager = new EmailManager();
      localReportWebAttr.setEmailManager(localEmailManager);
    }
    return localEmailManager;
  }

  public void setId(long paramLong)
  {
    this.id = paramLong;
  }

  public long getId()
  {
    return this.id;
  }

  public void setToAddress(String paramString)
  {
    this.toAddress = paramString;
  }

  public String getToAddress()
  {
    return this.toAddress;
  }

  public void setCcAddress(String paramString)
  {
    this.ccAddress = paramString;
  }

  public String getCcAddress()
  {
    return this.ccAddress;
  }

  public void setBccAddress(String paramString)
  {
    this.bccAddress = paramString;
  }

  public String getBccAddress()
  {
    return this.bccAddress;
  }

  public void setSubject(String paramString)
  {
    this.subject = paramString;
  }

  public String getSubject()
  {
    return this.subject;
  }

  public void setBodyContent(String paramString)
  {
    this.bodyContent = paramString;
  }

  public String getBodyContent()
  {
    if (this.bodyContent == null)
      return "";
    return this.bodyContent;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isAttr())
    {
      String str;
      if ((str = paramXMLableReader.getAttr("toAddress")) != null)
        setToAddress(str);
      if ((str = paramXMLableReader.getAttr("ccAddress")) != null)
        setCcAddress(str);
      if ((str = paramXMLableReader.getAttr("bccAddress")) != null)
        setBccAddress(str);
      if ((str = paramXMLableReader.getAttr("subject")) != null)
        setSubject(str);
      if ((str = paramXMLableReader.getAttr("bodyContent")) != null)
        setBodyContent(str);
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("EmailNotification").attr("toAddress", this.toAddress).attr("ccAddress", this.ccAddress).attr("bccAddress", this.bccAddress).attr("subject", this.subject).attr("bodyContent", this.bodyContent).end();
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof EmailNotification))
      return false;
    EmailNotification localEmailNotification = (EmailNotification)paramObject;
    return ((ComparatorUtils.equals(localEmailNotification.getBccAddress(), getBccAddress())) && (ComparatorUtils.equals(localEmailNotification.getCcAddress(), getCcAddress())) && (ComparatorUtils.equals(localEmailNotification.getToAddress(), getToAddress())));
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    EmailNotification localEmailNotification = (EmailNotification)super.clone();
    return localEmailNotification;
  }
}